package ui.magic.magiclibs.container.Frame;

import android.content.Context;
import android.graphics.Point;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import java.util.Vector;

import ui.magic.magiclibs.container.Base.Percent;
import ui.magic.magiclibs.container.Base.MagicInterface;
import ui.magic.magiclibs.container.Base.MagicType;

/**
 * Created by liushiyin on 2017/10/22.
 */

/*
*MagicUi中的Frame布局
* 在添加子节点时你可以指定子节点的宽度和高度相对于父节点的百分比大小
 */
public class FrameMagicUi extends FrameLayout implements MagicInterface{

 /********************以下参数用于指定父节点中子节点的对齐方式*****************/
    public static int LEFT=Gravity.LEFT;
    public static int TOP=Gravity.TOP;
    public static int CENTER=Gravity.CENTER;
    public static int RIGHT=Gravity.RIGHT;
    public static int BOTTOM=Gravity.BOTTOM;
 /****************************************************************************/
    private Point magicSize; //保存组件的大小
    private Vector<View> childList;
    private Vector<Percent> percentList;
    private MagicType type;

    public FrameMagicUi(@NonNull Context context) {
        super(context);
        init(MagicType.As_Child);
    }

    public FrameMagicUi(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(MagicType.As_Child);
    }

    public FrameMagicUi(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(MagicType.As_Child);
    }

    /*
     *进行一些初始化操作
    */
    private void init(MagicType type) {
        this.type=type;
        childList=new Vector<>();
        percentList=new Vector<>();
        magicSize=getWindowSize();
    }

    /*
    *在组件大小改变时，调整子节点大小
     */
    private void reOrderChildSize(){
        int count=childList.size();
        for(int i=0;i<count;++i){
            View view=childList.get(i);
            Percent percent=percentList.get(i);
            //change(view,percent.perx,percent.pery);
            super.removeView(view);
            LayoutParams params=(LayoutParams) view.getLayoutParams();
            addChildTmp(view,percent,-2);
        }
    }

    /*
  *设置UI组件大小，同时会调整子组件大小，自己不要轻易调用此函数，
  *而应有UI组件自身在适当的时候调用
  *@Param width：宽度
  *@Param height：高度
   */
    @Override
    public void setMagicSize(int width, int height) {
        magicSize.x=width;
        magicSize.y=height;
        reOrderChildSize();
    }

    /*
   *添加View子节点
   * @Param v：View子节点
   * @Param perWidth：0~100的浮点数表示子节点宽度为父节点宽度的百分比值
   * @Param perHeight：0~100的浮点数表示子节点宽度为父节点高度的百分比值
    */
    public void addChild(View v, float perWidth, float perHeight){
        addChild(v,perWidth,perHeight,CENTER);
    }

    /*
  *添加View子节点，并指定其对齐方式
  * @Param v：View子节点
  * @Param perWidth：0~100的浮点数表示子节点宽度为父节点宽度的百分比值
  * @Param perHeight：0~100的浮点数表示子节点宽度为父节点高度的百分比值
  * @Param align：子节点的对齐方式，可选参数为LEFT、RIGHT、TOP、BOTTOM、CENTER，
  * 也可以通过“|”操作符结合使用
   */
    public void addChild(View v, float perWidth, float perHeight,int align){
        Percent percent=new Percent(perWidth,perHeight,align);
        addChildTmp(v,percent,-1);
        childList.add(v);
        percentList.add(percent);
    }

    /*
    *addChildView的中间过程
     */
    private void addChildTmp(View child,Percent percent,int index){
        float dxw=percent.perx/MAX;
        float dxh=percent.pery/MAX;
        int realWidth=(int)(magicSize.x*dxw);
        int realHeight=(int)(magicSize.y*dxh);
        if(child instanceof MagicInterface){
            ((MagicInterface)child).setMagicSize(realWidth,realHeight);
        }
        LayoutParams params=new LayoutParams(realWidth,realHeight);
        params.setMargins(percent.left,percent.top,percent.right,percent.bottom);
        params.gravity=percent.gravity;
        super.addView(child,index,params);
    }

    /*
    *移除指定的View字节点
    * @Param v：指定的view节点
    * @return boolean：成功返回true，失败返回false
     */
    public boolean removeChild(View child){
        int index=childList.indexOf(child);
        if(index!=-1){
            super.removeView(child);
            childList.remove(index);
            percentList.remove(index);
            return true;
        }else{
            return false;
        }
    }

    public boolean changeChildSize(View child,float perWidth,float perHeight){
        int index=this.indexOfChild(child);
        if(index!=-1){
            //change(child,perWidth,perHeight);
            super.removeView(child);
            Percent percent=percentList.get(index);
            percent.perx=perWidth;
            percent.pery=perHeight;
            addChildTmp(child,percent,index);
            return true;
        }else {
            return false;
        }
    }

    /*
    *获取屏幕大小
    * @return Point：保存屏幕大小
     */
    public Point getWindowSize(){
        WindowManager wm=(WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
        Point point=new Point();
        wm.getDefaultDisplay().getSize(point);
        return  point;
    }

    /*
    *获取自身的大小
    * @return Point：返回组件大小
     */
    public Point getRealSize(){
        return magicSize;
    }

    public void setChildMargin(View child,int left,int top,int right,int bottom){
        int index=childList.indexOf(child);
        if(index!=-1){
            LayoutParams params=(LayoutParams) child.getLayoutParams();
            params.setMargins(left,top,right,bottom);
            Percent percent=percentList.get(index);
            percent.left=left;
            percent.top=top;
            percent.right=right;
            percent.bottom=bottom;
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if(type==MagicType.Self_Init){
            int width=getMeasuredWidth();
            int height=getMeasuredHeight();
            if(magicSize.x!=width||magicSize.y!=height){
                setMagicSize(width,height);
            }
        }
    }

    public void setSelfInit(){
        type=MagicType.Self_Init;
    }

    public void setAsChild(){
        type=MagicType.As_Child;
    }

    //以下函数标记为废弃，不建议使用。
    @Override
    @Deprecated
    public void addView(View child) {super.addView(child);}
    @Override
    @Deprecated
    public void addView(View child, int index) {super.addView(child,index);}
    @Override
    @Deprecated
    public void addView(View child, int width, int height) {super.addView(child, width, height);}
    @Override
    @Deprecated
    public void addView(View child, ViewGroup.LayoutParams params) {super.addView(child, params);}
    @Override
    @Deprecated
    public void addView(View child, int index, ViewGroup.LayoutParams params) {super.addView(child, index, params);}
    @Override
    @Deprecated
    public void removeView(View view) {super.removeView(view);}
    @Override
    @Deprecated
    public void removeViewAt(int index) {super.removeViewAt(index);}
    @Override
    @Deprecated
    public void removeViews(int start, int count) {super.removeViews(start, count);}
    @Override
    @Deprecated
    public void removeAllViews() {super.removeAllViews();}
}
